<template>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <el-form :inline="true" :model="params.entity" style="padding-bottom: 0px;">
          <el-form-item label="ip">
            <el-input v-model="params.entity.ip" placeholder="请输入ip"/>
          </el-form-item>
          <el-form-item label="请求方式">
            <el-select v-model="params.entity.method" placeholder="请输入请求方式" clearable="true">
              <el-option v-for="item in REQUEST_LIST" :label="item.label" :value=item.value>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :icon="Search" @click="searchData">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <el-table v-loading="loading"
              :data="tableData.list" style="width: 100%" height="500"
              :default-sort="{ prop: 'date', order: 'descending' }">
      <el-table-column prop="userName" label="执行用户" width="120" show-overflow-tooltip/>
      <el-table-column prop="ip" label="IP地址" width="120" show-overflow-tooltip/>
      <el-table-column prop="url" label="请求地址" width="160" show-overflow-tooltip/>
      <el-table-column prop="method" label="请求方式" width="120" show-overflow-tooltip/>
      <el-table-column prop="className" label="类" show-overflow-tooltip/>
      <el-table-column prop="methodName" label="方法名称" width="120" show-overflow-tooltip/>
      <el-table-column prop="args" label="请求参数" width="160" show-overflow-tooltip/>
      <el-table-column prop="createTime" label="创建时间" width="160" show-overflow-tooltip/>
      <!--      <el-table-column prop="remark" label="日志备注" width="160" show-overflow-tooltip/>-->
      <el-table-column fixed="right" label="操作" width="100" show-overflow-tooltip>
        <template #header>
          <el-input v-model="params.search" @keyup.enter.native="searchData" placeholder="搜索日志"/>
        </template>
        <template #default="scope">
          <el-button link type="danger" :icon="Delete" size="small" @click="showDeleteDialog(scope.row.id)">删除
          </el-button>
          <!--          <el-button link type="primary" :icon="Edit" size="small" @click="editDialog(scope.row)">编辑</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <el-pagination v-model:currentPage="params.current" v-model:page-size="params.size"
                   :page-sizes="[100, 200, 300, 400]" layout="sizes, prev, pager, next" :total="tableData.total"
                   @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
  </el-card>

  <!-- 新增或者修改页面-->
  <LogAddOrEdit
      @closeEditDialog="closeEditDialog"
      :addOrEditVisible="addOrEditVisible"
      :ruleForm="ruleForm"
      :addDataType="addDataType"></LogAddOrEdit>
</template>

<script lang="ts" setup>
import {ElTable, ElMessage, ElMessageBox} from 'element-plus'
import {onMounted, computed, reactive, ref} from 'vue'
import {Params} from "@/apis/model/common/common.interfaces"
import {Delete, Edit, Search, Share, Upload, Plus, Setting} from '@element-plus/icons-vue'
import LogAddOrEdit from './LogAddOrEdit.vue'
import {Log} from "@/apis/model/system/LogModel";
import {deleteLogById, logPage} from "@/apis/http/system/LogHttp";
import {REQUEST_LIST} from "@/util/common.util";

// 查询
const params: Params = reactive({
  current: 1,
  size: 10,
  search: '',
  entity: {} as Log
})

const ruleForm = ref<Log>({} as Log)

// AddOrEditVisible
const addOrEditVisible = ref(false)

// table 表数据
const tableData: any = ref({});

// 加载数据
const loading = ref(true)
const addDataType = ref('add');

/**
 * 初始化数据
 */
const initList = () => {
  loading.value = true
  logPage(params).then(res => {
    const code = res.code
    const message = res.message
    const data = res.data
    if (code == 200) {
      tableData.value = data
    }
    loading.value = false
  })
}

/**
 * search 查询数据
 */
const searchData = () => {
  initList()
}

/**
 * 每页数据量改变查询数据
 * @param val
 */
const handleSizeChange = (val: number) => {
  params.size = val
  initList()
}

/**
 * 分页查询数据
 * @param val
 */
const handleCurrentChange = (val: number) => {
  params.current = val
  initList()
}

/**
 * 添加弹出
 */
const addDialog = () => {
  addOrEditVisible.value = true
  ruleForm.value = {} as Log
  addDataType.value = 'add'
}

//修改弹窗
const editDialog = (row: Log) => {
  addOrEditVisible.value = true
  ruleForm.value = row
  addDataType.value = 'edit'
}

// 关闭修改弹窗
const closeEditDialog = (e: any) => {
  addOrEditVisible.value = e.addOrEditVisible
  if (e.refushList = true) {
    params.current = 1;
    initList();
  }
}

/**
 * 弹出删除对话框
 */
const showDeleteDialog = (id: number) => {
  ElMessageBox.confirm(
      '是否删除角色?',
      '提醒',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        deleteLogById(id).then(res => {
          const code = res.code;
          if (code == 200) {
            ElMessage({
              message: '删除数据成功！',
              type: 'success',
            })
            initList();
          } else {
            ElMessage.error('删除数据失败！');
          }
        })
      })
      .catch(() => {
        // ElMessage({
        //   type: 'info',
        //   message: 'Delete canceled',
        // })
      })
}

onMounted(() => {
  initList()
})
</script>

<style>
</style>
